package test.reflect;

import java.awt.geom.GeneralPath;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;

import base.*;

public class FieldDemo extends BaseObject{
	
	static final String TAG = "FieldDemo";
	
	public static  void demo(String className) throws Exception{
		Class<?> class1 = null;  
        class1 = Class.forName(className);  
          
        //取得父类名称  
        Class<?>  superClass = class1.getSuperclass();  
        mLog.i(TAG, className + "类的父类名: " + superClass.getName());  
          
        mLog.i(TAG, "===============================================");  
          
        StringBuilder sb = new StringBuilder();  
        Field[] fields = class1.getDeclaredFields();  
        for (int i = 0; i < fields.length; i++) {  
        	sb.delete(0, sb.length());
        	sb.append("类中的第" + i  + "个成员" + fields[i]);
//        	sb.append("\t getDeclaringClass==" + fields[i].getDeclaringClass());
//        	sb.append("\t getDeclaredAnnotations==" + fields[i].getDeclaredAnnotations());
        	sb.append("\t getGenericType==" + fields[i].getGenericType());
        	sb.append("\t getType==" + fields[i].getType());
        	sb.append("\t getModifiers==" + fields[i].getModifiers());
        	sb.append("\t getName==" + fields[i].getName());
        	
//        	sb.append("\t isSynthetic==" + fields[i].isSynthetic());
//        	sb.append("\t toGenericString==" + fields[i].toGenericString());
        	if (fields[i].getType().getPackage() != null){
        		sb.append("\t packageName==" + fields[i].getType().getPackage().getName());
        		if (fields[i].getType().getPackage().getName().equals("test.reflect")){
        			demo(fields[i].getType().getName());
        		}
        	}
        	
            mLog.i(TAG, sb.toString());  
            mLog.i(TAG, "----------------------------------------------------------------------------------------------------");
            instancelActualTypeArgument(fields[i].getGenericType());
            mLog.i(TAG, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            mLog.i(TAG, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println("\n\n");
            
        }  
//        mLog.i(TAG, "----------------------------------------------------------------------------------------------------");  
        
	}
	
	
	//判断实际的Type类型
	public static void instancelActualTypeArgument(Type type) throws Exception{
		if (type instanceof ParameterizedType){
			mLog.i(TAG, "ParameterizedType instanceOf " + type + "\t <<<<<<<<<<<<<<<<<<< ");
			Type[] typeArguments = ((ParameterizedType)type).getActualTypeArguments();
			for (int i=0; i<typeArguments.length; i++){
				if (typeArguments[i] instanceof TypeVariable){  //类型变量
					mLog.i(TAG, "第" + i + "个泛型参数是 类型变量 \t" + typeArguments[i] + ", 无法实例化");
				}else if (typeArguments[i] instanceof WildcardType) { //通配符表达式
					mLog.i(TAG, "第" + i + "个泛型参数是 通配符表达式 \t" + typeArguments[i] + ", 无法实例化");
				}else if (typeArguments[i]  instanceof Class){
					mLog.i(TAG, "第" + i + "个泛型参数是 class\t" + typeArguments[i] + ", 可以实例化");
				}
			}
			
		}else if (type instanceof GenericArrayType){
			mLog.i(TAG, "GenericArrayType instanceof " + type + "\t>>>>>>>>> 参数化类型数组或类型变量数组");
			Type componentType = ((GenericArrayType)type).getGenericComponentType();
			if (componentType instanceof TypeVariable){
				mLog.i(TAG, ">>>>>>>>>  " + componentType + "\t 类型变量，无法实例化");
			}else {
				instancelActualTypeArgument(componentType);
			}
			
		}else if (type instanceof TypeVariable){
			mLog.i(TAG, "TypeVariable instanceof " + type + "\t ===== 类型变量");
			
		}else if (type instanceof WildcardType){
			mLog.i(TAG, "WildcardType instanceof "  + type + "\t^^^^^^^^  通配符表达式");
		}else  if (type instanceof Class){
			mLog.i(TAG, "class instanceof  "  + type + " ################# 具体的类，而不是泛型类型");
		}else {
			mLog.i(TAG, "exception  没有对应的Type类型  type = " + type);
			throw new Exception();
		}
	}
	
	
	
}
